var GraceJS = require("../../Grace.JS/grace.js");
import { apiServer } from '../../common.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        page: 1,
        limit: 20,
        loadingType: 1,
        selectSortMenu: ['默认排序', '最新分享', '最多浏览'],
        sortList: ['', 'new', 'look'],
        sort: '',
        sortType: false,
        selectTagsMenu: ['全部分类 (0)'],
        type_code: '',
        TagsList: [],
        TagsType: false,
        sort:'',
        type_id:''
    },
    showSortMenu: function () { this.setData({ sortType: true }); },
    closeSortMenu: function () { this.setData({ sortType: false }); },
    selectSortMenu: function (index) {
        var that = this;
        that.setData({
            sort: that.data.sortList[index.detail],
            type_id: that.data.type_id,
            page: 1,
            limit: 20,
            list: [],
            loadingType: 1
        }, function () {
            that.getList()
        })
    },

    showTagsMenu: function () { this.setData({ TagsType: true }); },
    closeTagsMenu: function () { this.setData({ TagsType: false }); },
    selectTagsMenu: function (index) {
        var that = this;
        that.setData({
            sort: that.data.sort,
            type_id: that.data.TagsList[index.detail].id,
            page: 1,
            limit: 20,
            list: [],
            loadingType: 1
        }, function () {
            that.getList()
        })
    },

    getTypeList: function(){
        var that = this;
        GraceJS.get(
            apiServer + 'resume.News/codeType', 
            {}, {},
            function (res) {
                if (res.code == 0) {
                    console.log(res)
                    that.setData({
                        selectTagsMenu: res.data.arrs,
                        TagsList: res.data.list,
                    })
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList();
        this.getTypeList();
    },
    getList: function () {
        var that = this;
        wx.showLoading({
            title: 'Loading...',
        })
        GraceJS.get(
            apiServer + 'resume.News/codeList', {
            page: that.data.page,
            limit: that.data.limit,
            sort: that.data.sort,
            type_id: that.data.type_id,
        }, {},
            function (res) {
                if (res.code == 0) {
                    var nowList = GraceJS.arrayConcat(that.data.list, res.data);
                    that.setData({
                        list: nowList,
                        isEmpty: false,
                        page: that.data.page + 1,
                        loadingType: res.data.length < that.data.limit ? 3 : 1,
                    }, function () {
                        if (that.data.list.length == 0) {
                            that.setData({
                                isEmpty: true
                            })
                        }
                        wx.hideLoading();
                    })
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        //避免多次触发
        if (that.data.loadingType == 3) {
            return;
        }
        that.getList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '功能代码片段分享，独立功能，复制即用',
            path: '/pages/code-list/index',
        }
    }
})